<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2020/9/12
 * Time: 00:54
 */

namespace app\web\controller;

use app\common\lib\IAuth;
use app\common\lib\Jssdk;
use think\Controller;
use think\Env;

/**
 * 占星师挑战赛
 * @version 2.3.0开发
 * Class Challenge
 * @package app\web\controller
 */
class Challenge extends Controller
{
    public function index()
    {
        // 检查运行环境：true => 运行在APP内 , false => 在APP外的请求
        $rn = $this->checkRuntimeEnvironment();

        $domain = Env::get('host.domain');

        $header = $this->request->header();
        // 微信二次分享
        $signPackage = (new Jssdk())->getSignPackage();

        return $this->fetch('index', [
            'rn' => $rn ? 1 : 0,
            'domain' => $domain,
            'header' => $header,
            'title' => '万元现金奖励！准了占星pk挑战邀您参赛！',
            'share_title' => '万元现金奖励！准了占星pk挑战邀您参赛！',
            'share_des' => '做最强占星师赢万元现金',
            'share_url' => url('index'),
            'signPackage' => $signPackage
        ]);
    }

    /**
     * 检测运行环境
     * 在APP内容使用时必须登录状态
     */
    protected function checkRuntimeEnvironment()
    {
        // 首先需要获取headers
        $this->headers = $this->request->header();
        if (isset($this->headers['sign'])
            && strpos(config('app.app_types'), $this->headers['app-type']) !== true
            && IAuth::checkSignPass($this->headers))
        {
            return true;
        }

        return false;
    }
}
